﻿
(function (global, $) {
    //居中元素
    $.fn.center = function () {
        return this.css({
            'top': ($(window).height() - this.height()) / 2 + $(window).scrollTop(),
            'left': ($(window).width() - this.width()) / 2 + $(window).scrollLeft()
        });

    };

    //移除loading

    function _hideLoading() {
        $('#loadingBox').hide(200);

    }

    function renderTemplate(msg, type, delay, modal) {

        //默认图片路径 Loading
        var img = 'loading.gif';
        //根据类型 动态创建提示框
        switch (type) {
            case 'info':
                img = 'info.png';
                break;
            default:
                img = 'loading.gif';
                break;
        }
        //遮罩层

        if (modal && modal == 1) {
            var loadingMasks = '<div id="loadingBox" style=""><div id="loadingDiv" style=" position: absolute;width: 100%;height: 100%;top: 0;  bottom: 0;background: #000; opacity: 0.1; z-index: 999998;"></div><div id="loadingElem" style=" font-size: 12px;color: #09f;text-align: left;background:#fff;padding:10px;box-shadow:1px 1px 5px #ccc;border-radius:2px; margin: 0 auto; position: absolute;z-index: 999999;"><img width="32" height="32" src="/Template/Default/javascript/loading/' + img + '" style="float:left;"  alt="请稍等..." /><p style="float:left; margin-left:6px;line-height:27px;"  ></p></div> </div>';
            if (!$('#loadingBox').length) {
                $(loadingMasks).appendTo('body');
            }
            $('#loadingDiv').css({ opacity: .1 });
            $('#loadingElem').find('p').html(msg);
            $('#loadingBox').fadeIn(200);

            if (delay && $.isNumeric(delay)) {
                setTimeout(hideLoading, delay);
            }
        } else {
            var loadingMask = '<div id="msg_Elem" style="background:#fff;padding:10px;box-shadow:1px 1px 5px #ccc;border-radius:2px; margin: 0 auto; position: absolute;z-index: 999999;"><img width="32" height="32" src="/Template/Default/javascript/loading/' + img + '" style="float:left;"  alt="请稍等..." /><p style="float:left; margin-left:6px;line-height:27px; font-size: 12px;color: #09f;text-align: left;"  ></p></div> ';

            if (!$('#msg_Elem').length) {
                $(loadingMask).appendTo('body');
            }
            $('#loadingDiv').css({ opacity: .1 });
            $('#msg_Elem').find('p').html(msg);
            $('#msg_Elem').fadeIn(200);

            if (delay && $.isNumeric(delay)) {
                setTimeout(function () {
                    $('#msg_Elem').fadeOut(200);
                }, delay);
            }
        }


    }

    //重新计算

    function resizeMask() {
        $('#loadingDiv').css({ width: $(document).width(), height: $(document).height() });
        $('#loadingElem,#msg_Elem').center();
    }

    //显示loading

    function _showLoading(msg, type, delay, modal) {
        modal = modal || 1;
        msg = msg || "正在处理中，请稍候....";
        renderTemplate(msg, type, delay, modal);
        //窗口移动计算
        $(window).bind('resize scroll', function () {
            setTimeout(function () {
                resizeMask();
            }, 10);
        });
        resizeMask();
    }

    //全局消息

    function _msg(html, delay) {
        delay = delay || 3500;
        _showLoading(html, 'info', delay, 2);
    }

    //显示loading
    global.showLoading = _showLoading;
    //隐藏
    global.hideLoading = _hideLoading;
    //msg
    global.msg = _msg;

})(this, jQuery);



